package com.xlh.server.protocol.support.json;

import android.text.Html;
import android.widget.LinearLayout;

import com.xlh.Utils.FUti;
import com.xlh.Utils.FontHandler;
import com.xlh.Utils.GsonManager;
import com.xlh.Utils.UrlFontHandler;
import com.xlh.bean.CharBean;
import com.xlh.bean.ProtocolObject.Window;
import com.xlh.interf.IActivity;
import com.xlh.interf.ITakeMessage;

import net.nightwhistler.htmlspanner.HtmlSpanner;

/**
 * Created by Administrator on 2018/7/2.
 */

public class ChannelServer implements ITakeMessage {

    private IActivity atv;
    private String hasType = "channel";
    private Window protccoObj;
    public ChannelServer(IActivity atv){
        this.atv = atv;


    }

    @Override
    public void takeMessage(String t,String s){

        if(!t.equals(this.hasType)) return;

        protccoObj =  GsonManager.gsmgr.jsonToAnyObject(s, Window.class);

        if(protccoObj==null) return;

        final ChannelServer me = this;

        CharBean charBean = new CharBean();


        if(this.atv.getActivity().mChatBean.size()>20){
            this.atv.getActivity().mChatBean.clear();
        }


        String text = protccoObj.getText();


       // FontHandler htmlHandlerChatList =  new FontHandler(this.atv.getActivity());

        charBean.setmContent(this.atv.getActivity().getHtmlSpanner().fromHtml(text));
        // 处理数据

        this.atv.getActivity().mChatBean.add(charBean);
        this.atv.getActivity().mChatAdapter.setData(this.atv.getActivity().mChatBean);
        //扩充父容器高度
        LinearLayout.LayoutParams mp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, FUti.dip2px(this.atv.getActivity(),60));
        LinearLayout lay = (LinearLayout)this.atv.getActivity().mChatList.getParent();
        lay.setLayoutParams(mp);



        this.atv.getActivity().mChatList.post(new Runnable() {
            @Override
            public void run() {
                // Select the last row so it will scroll into view...
                me.atv.getActivity().mChatList.setSelection(me.atv.getActivity().mChatBean.size() - 1);
            }
        });

    }

    @Override
    public boolean takeMessage(String s) {
        return false;
    }

    @Override
    public void init() {

    }

    @Override
    public void atViewLoadAfterInit() {

    }

}
